package com.atguigu.flink02;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.RestOptions;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;

/**
 * @author Felix
 * @date 2024/2/19
 * 该案例演示了Flink的并行度
 *  如果本地运行程序的时候没有指定并行度，默认当前系统CPU的线程数
 * 并行度的设置方式
 *  全局设置
 *      env.setParallelism()
 *  单独指定算子的并行度
 *      .setParallelism(3);
 *
 *  在flink-conf.xml配置文件中指定
 *      parallelism.default: 1
 *  在提交应用的时候，通过参数指定
 *      -p 数量
 *  单独指定算子的并行度 > 全局设置 > 通过参数指定 > 在flink-conf.xml配置文件中指定
 */
public class Flink01_par {
    public static void main(String[] args) throws Exception {
        //TODO 1.指定流处理环境
//        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        Configuration conf = new Configuration();
        //指定本地WebUI的端口
        conf.set(RestOptions.BIND_PORT,"8088");
        StreamExecutionEnvironment env = StreamExecutionEnvironment.createLocalEnvironmentWithWebUI(conf);
        //设置的全局并行度
        env.setParallelism(2);
        //全局禁用算子链
        env.disableOperatorChaining();
        //TODO 2.从指定的网络端口读取数据
        DataStreamSource<String> socketDS = env.socketTextStream("hadoop102", 8888);
        //TODO 3.扁平化处理  获取一个个单词
        SingleOutputStreamOperator<String> wordDS = socketDS.flatMap(
                new FlatMapFunction<String, String>() {
                    @Override
                    public void flatMap(String lineStr, Collector<String> out) throws Exception {
                        String[] words = lineStr.split(" ");
                        for (String word : words) {
                            out.collect(word);
                        }
                    }
                }
        );
        //TODO 4.将单词封装为二元组
        SingleOutputStreamOperator<Tuple2<String, Integer>> tupleDS = wordDS.map(
                new MapFunction<String, Tuple2<String, Integer>>() {
                    @Override
                    public Tuple2<String, Integer> map(String word) throws Exception {
                        return Tuple2.of(word, 1);
                    }
                }
//        ).disableChaining();
        ).startNewChain();
        //TODO 5.按照单词分组
        KeyedStream<Tuple2<String, Integer>, String> keyedDS = tupleDS.keyBy(
                new KeySelector<Tuple2<String, Integer>, String>() {
                    @Override
                    public String getKey(Tuple2<String, Integer> tup) throws Exception {
                        return tup.f0;
                    }
                }
        );
        //TODO 6.计数
        SingleOutputStreamOperator<Tuple2<String, Integer>> sumDS = keyedDS.sum(1);
        //TODO 7.打印输出
        sumDS.print();
        //TODO 8.提交作业
        env.execute();
    }
}
